<template>
  <div class="app-container">
    <!-- 搜索 -->
    <div class="filter-container">
      <div v-if="checkPermission(['log@DELETE'])" class="filter-button">
        <el-button
          v-permission="['log@DELETE']"
          type="danger"
          icon="el-icon-delete"
          @click="selectDelete"
        >删除
        </el-button>
      </div>
      <div class="filter-form">
        <el-form :model="filter" :inline="true" size="medium">
          <el-form-item label="">
            <el-input v-model="filter.keyword" placeholder="用户名" />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="filter.date"
              value-format="yyyy-MM-dd"
              type="daterange"
              size="mini"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
        </el-form>
        <div>
          <el-button v-permission="['log@GET']" @click="search">
            搜索
          </el-button>
          <el-button
            v-permission="['log@GET']"
            @click="resetForm"
          >重置
          </el-button>
        </div>
      </div>
    </div>
    <div v-loading="listLoading" class="content-container">
      <el-table
        :cell-style="{ textAlign: 'center' }"
        :data="list"
        border
        style="width: 100%"
        row-key="id"
        size="mini"
        :header-cell-class-name="headerStyle"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="用户名" />
        <el-table-column prop="url" label="接口地址" />
        <el-table-column prop="method" label="请求类型">
          <template slot-scope="{ row }">
            <el-tag>{{ row.method.toUpperCase() }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="ip" label="ip" />
        <el-table-column prop="input" label="输入">
          <template v-if="row.input!=='[]'" slot-scope="{ row }">
            <el-popover trigger="hover" placement="top">
              <pre>{{ JSON.stringify(JSON.parse(row.input), null, 4) }}</pre>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ JSON.stringify(JSON.parse(row.input), null, 4) }}</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column v-if="checkPermission(['log@DELETE'])" label="操作" width="250px" fixed="right">
          <template slot-scope="{ row }">
            <el-button
              v-permission="['log@DELETE']"
              type="text"
              class="button-red"
              @click="del(row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="filter.page"
      :limit.sync="filter.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import layoutMinix from '@/layout/components/mixins/layout'
import { getLogList, delLog } from '@/api/auth'
import Pagination from '@/components/Pagination'
import checkPermission from '@/utils/permission' // 权限判断函数
export default {
  name: 'Log',
  components: { Pagination },
  mixins: [layoutMinix],
  data() {
    return {
      total: 0,
      filter: {
        page: 1,
        limit: 10,
        keyword: '',
        date: ''
      },
      list: [],
      listLoading: false,
      multipleSelection: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    checkPermission,
    headerStyle({ row, rowIndex }) {
      return 'table-th'
    },
    /**
     * 获取列表
     */
    getList() {
      this.listLoading = true
      getLogList(this.filter).then((response) => {
        const { list, total } = response.data
        this.list = list
        this.total = total
        this.listLoading = false
      })
    },
    search() {
      this.filter.page = 1
      this.getList()
    },
    resetForm() {
      this.filter.page = 1
      this.filter.keyword = ''
      this.filter.date = ''
      this.getList()
    },
    del(item) {
      this.$confirm('此操作将永久删除该日志, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delLog(item.id).then(() => {
            this.$message.success('删除成功')
            this.getList()
          })
        })
    },
    selectDelete() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择需要删除的数据')
      } else {
        const ids = []
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i]['id'])
        }
        this.del({ id: ids.join(',') })
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
<style lang="scss">
.json-pre {
   background-color: #e8f4ff;
    border-color: #d1e9ff;
    color: #1890ff;
}
</style>

